package com.zhuang.work0430.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuang.work0430.entity.User;
import com.zhuang.work0430.service.UserService;
import com.zhuang.work0430.util.ResponseJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhuang
 * @since 2021-04-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("findUsers.json/{pageNo}")
    public ResponseJSON findUsers(@RequestBody User user, @PathVariable("pageNo") Integer pageNo) {
        ResponseJSON json = null;
        Page<User> userPage = new Page<>(pageNo, 4);
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        Page<User> users = userService.getBaseMapper().selectPage(userPage, wrapper);
        if (users.getRecords() == null) {
            return json = new ResponseJSON("500", "error", null);
        } else {
            return json = new ResponseJSON("200", "success", users);
        }
    }

    @RequestMapping("getUserById.do/{id}")
    public ResponseJSON getUserById(@PathVariable("id") Integer id) {
        ResponseJSON json = null;
        User user = new User();
        user.setId(id);
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        User user1 = userService.getBaseMapper().selectOne(wrapper);
        if (user1 != null) {
            return json = new ResponseJSON("200", "success", user1);
        }
        return json = new ResponseJSON("500", "error", null);
    }

    @RequestMapping("insert.do")
    public ResponseJSON insert(@RequestBody(required = false) User user) {
        ResponseJSON json = null;
        int insert = userService.getBaseMapper().insert(user);
        if (insert > 0) {
            return json = new ResponseJSON("200", "success", null);
        }
        return json = new ResponseJSON("500", "error", null);
    }

    @RequestMapping("update.do")
    public ResponseJSON update(@RequestBody(required = false) User user) {
        ResponseJSON json = null;
        int update = userService.getBaseMapper().updateById(user);
        if (update > 0) {
            return json = new ResponseJSON("200", "success", null);
        }
        return json = new ResponseJSON("500", "error", null);
    }

    @RequestMapping("delete.do/{id}")
    public ResponseJSON delete(@PathVariable("id") Integer id) {
        ResponseJSON json = null;
        int i = userService.getBaseMapper().deleteById(id);
        if (i > 0) {
            return json = new ResponseJSON("200", "success", null);
        }
        return json = new ResponseJSON("500", "error", null);
    }


}

